package net.hands.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 
* 说明:mac地址
* <br>
 * <p>这是一个开源的软件，请在LGPLv3下合法使用、修改或重新发布。</p>
 * @author 陈侃(cknet@126.com, 13910090885)
 *         http://code.google.com/p/simpleframework/
 *         http://www.simpleframework.net
 */
public class MacAddress {
	/**
	 * mac地址格式
	 */
	static private Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",
			Pattern.CASE_INSENSITIVE);
  /**
   * windows 命令集合
   */
	static final String[] windowsCommand = { "ipconfig", "/all" };
   /**
    * linux 命令集合
    */
	static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
   /**
   * 说明:获得所有mac地址
   * @return List<String>
   * @throws IOException
   * @update 2012-9-24 下午6:18:14
    */
	public final static List<String> getMacAddresses() throws IOException {
		final List<String> macAddressList = new ArrayList<String>();
      //获得命令执行后信息读取流
		final BufferedReader reader = getMacAddressesReader();
		for (String line = null; (line = reader.readLine()) != null;) {//循环行
			final Matcher matcher = macPattern.matcher(line);//匹配mac格式
			if (matcher.matches()) {//匹配成功
				macAddressList.add(matcher.group(1).replaceAll("[-:]", ""));//获得mac地址添加到mac地址集合中
			}
		}
		reader.close(); //关闭流
		return macAddressList; //返回mac地址集合
	}
   /**
   * 说明:获得第一个mac地址
   * @return String
   * @throws IOException
   * @update 2012-9-24 下午6:18:34
    */
	public final static String getMacAddress() throws IOException {
		return getMacAddress(0);
	}
   /**
   * 说明:返回mac地址
   * @param nicIndex 索引
   * @return String
   * @throws IOException
   * @update 2012-9-24 下午6:40:30
    */
	public final static String getMacAddress(final int nicIndex) throws IOException {
		//获得命令执行后信息读取流
		final BufferedReader reader = getMacAddressesReader();
		int nicCount = 0;
		for (String line = null; (line = reader.readLine()) != null;) {
			final Matcher matcher = macPattern.matcher(line);
			if (matcher.matches()) { //格式匹配成功
				if (nicCount == nicIndex) { //判断索引数是否与计数器相同
					reader.close(); //关闭读取流
					return matcher.group(1).replaceAll("[-:]", "");
				}
				nicCount++;
			}
		}
		reader.close();//返回索引制定mac地址
		return null;
	}
   /**
   * 说明:获得mac地址缓存读取流
   * @return BufferedReader
   * @throws IOException
   * @update 2012-9-24 下午6:32:21
    */
	private static BufferedReader getMacAddressesReader() throws IOException {
		final String[] command;
		final String os = System.getProperty("os.name");//获得本机系统

		if (os.startsWith("Windows")) {//为windws command等于window命令 
			command = windowsCommand;
		} else if (os.startsWith("Linux")) { //为linux command等于linux命令 
			command = linuxCommand;
		} else {
			throw new IOException("Unknown operating system: " + os);
		}
		final Process process = Runtime.getRuntime().exec(command);//执行命令
		//初始个线程
		new Thread() {

			@Override
			public void run() {
				try {
					//获得错误信息流
					final InputStream errorStream = process.getErrorStream();
					while (errorStream.read() != -1) {
						;
					}
					errorStream.close(); //关闭流
				} catch (final IOException e) {
					e.printStackTrace();
				}
			}

		}.start();
      //用缓冲读取流 获得命令执行后输入流信息
		return new BufferedReader(new InputStreamReader(process.getInputStream()));
	}
}
